<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>脱敏 | 一枚前端小菜鸟的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="场景全栈项目需要用到脱敏的地方 使用介绍 v1.0 只能匹配数字，satr和end代表需要隐藏的起始位置（satr&#x3D;1,end&#x3D;5  &#x3D;&gt; a****aaaaaaa）12345678910111213141516171819202122232425262728293031323334353637&#x2F;* * 替换字符串中的某几位（如：18351800598 --&gt; 183****0598）">
<meta property="og:type" content="article">
<meta property="og:title" content="脱敏">
<meta property="og:url" content="https://gitee.com/weiyayun1995/weiyayun1995.git/2021/06/18/%E8%84%B1%E6%95%8F/index.html">
<meta property="og:site_name" content="一枚前端小菜鸟的博客">
<meta property="og:description" content="场景全栈项目需要用到脱敏的地方 使用介绍 v1.0 只能匹配数字，satr和end代表需要隐藏的起始位置（satr&#x3D;1,end&#x3D;5  &#x3D;&gt; a****aaaaaaa）12345678910111213141516171819202122232425262728293031323334353637&#x2F;* * 替换字符串中的某几位（如：18351800598 --&gt; 183****0598）">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-06-18T05:48:10.000Z">
<meta property="article:modified_time" content="2021-06-18T05:51:58.674Z">
<meta property="article:author" content="yywei">
<meta property="article:tag" content="菜鸟,前端,js,react,vue">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="一枚前端小菜鸟的博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">一枚前端小菜鸟的博客</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">yywei的遇坑记录</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS 订阅"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="搜索"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/weiyayun1995/weiyayun1995.git"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-脱敏" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/06/18/%E8%84%B1%E6%95%8F/" class="article-date">
  <time class="dt-published" datetime="2021-06-18T05:48:10.000Z" itemprop="datePublished">2021-06-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      脱敏
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="场景"><a href="#场景" class="headerlink" title="场景"></a>场景</h2><p>全栈项目需要用到脱敏的地方</p>
<h2 id="使用介绍"><a href="#使用介绍" class="headerlink" title="使用介绍"></a>使用介绍</h2><ul>
<li>v1.0 只能匹配数字，satr和end代表需要隐藏的起始位置（satr=1,end=5  =&gt; a****aaaaaaa）<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 替换字符串中的某几位（如：18351800598 --&gt; 183****0598）(朱育仪手笔20190726)</span></span><br><span class="line"><span class="comment"> * @param  &#123;Number&#125;         [start=0]     [脱敏字符串起始位置]</span></span><br><span class="line"><span class="comment"> * @param  &#123;Number&#125;         [end=0]       [脱敏字符串终止位置]</span></span><br><span class="line"><span class="comment"> * @param  &#123;String&#125;         [str=&#x27;&#x27;]      [脱敏字符串]</span></span><br><span class="line"><span class="comment"> * @return &#123;String&#125;         [symbol=&#x27;*&#x27;]  [处理成功后的脱敏字符串]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> replaceStrByChar = <span class="function">(<span class="params">start = <span class="number">0</span>, end = <span class="number">0</span>, str = <span class="string">&#x27;&#x27;</span>, symbol = <span class="string">&#x27;*&#x27;</span></span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 尾部长度 如：183****0598中的 0598</span></span><br><span class="line">    <span class="keyword">let</span> tailLength = str.length - end;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 切割长度 如：183****0598中的 ****</span></span><br><span class="line">    <span class="keyword">let</span> inciseLength = end - start;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 判断传参是否合法</span></span><br><span class="line">    <span class="keyword">if</span> (tailLength &lt; <span class="number">0</span> || inciseLength &lt; <span class="number">0</span> || start &lt; <span class="number">0</span> || end &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 正则语句 如：/(\d&#123;3&#125;)\d&#123;4&#125;(\d&#123;4&#125;)/</span></span><br><span class="line">    <span class="keyword">let</span> regex = <span class="keyword">new</span> <span class="built_in">RegExp</span>(</span><br><span class="line">        <span class="string">&#x27;(\\d&#123;&#x27;</span> + start + <span class="string">&#x27;&#125;)\\d&#123;&#x27;</span> + inciseLength + <span class="string">&#x27;&#125;(\\d&#123;&#x27;</span> + tailLength + <span class="string">&#x27;&#125;)&#x27;</span></span><br><span class="line">    );</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 循环生成 脱敏符号</span></span><br><span class="line">    <span class="keyword">let</span> mySymbol = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; inciseLength; i++) &#123;</span><br><span class="line">        mySymbol += symbol;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 正则匹配</span></span><br><span class="line">    <span class="keyword">let</span> finalStr = str.replace(regex, <span class="string">&#x27;$1&#x27;</span> + mySymbol + <span class="string">&#x27;$2&#x27;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 返回值</span></span><br><span class="line">    <span class="keyword">return</span> finalStr;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
</ul>
<ul>
<li>v2.0 全能匹配，satr和end代表保留字符的数量（satr=1,end=1  =&gt; a***********a）<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">desensitization</span>(<span class="params">start = <span class="number">0</span>, end = <span class="number">0</span>, str = <span class="string">&#x27;&#x27;</span>, symbol = <span class="string">&#x27;*&#x27;</span></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 尾部长度 如：183****0598中的 0598</span></span><br><span class="line">    <span class="keyword">let</span> tailLength = end;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 切割长度 如：183****0598中的 ****</span></span><br><span class="line">    <span class="keyword">let</span> inciseLength = str.length - (end + start);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 判断传参是否合法</span></span><br><span class="line">    <span class="keyword">if</span> (tailLength &lt; <span class="number">0</span> || inciseLength &lt; <span class="number">0</span> || start &lt; <span class="number">0</span> || end &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 正则语句 如：/(\d&#123;3&#125;)\d&#123;4&#125;(\d&#123;4&#125;)/</span></span><br><span class="line">    <span class="keyword">let</span> regex = <span class="keyword">new</span> <span class="built_in">RegExp</span>(</span><br><span class="line">        <span class="string">&#x27;(\[\\d\\D\]&#123;&#x27;</span> + start + <span class="string">&#x27;&#125;)\[\\d\\D\]&#123;&#x27;</span> + inciseLength + <span class="string">&#x27;&#125;(\[\\d\\D\]&#123;&#x27;</span> + tailLength + <span class="string">&#x27;&#125;)&#x27;</span></span><br><span class="line">    );</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 循环生成 脱敏符号</span></span><br><span class="line">    <span class="keyword">let</span> mySymbol = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; inciseLength; i++) &#123;</span><br><span class="line">        mySymbol += symbol;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 正则匹配</span></span><br><span class="line">    <span class="keyword">let</span> finalStr = str.replace(regex, <span class="string">&#x27;$1&#x27;</span> + mySymbol + <span class="string">&#x27;$2&#x27;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 返回值</span></span><br><span class="line">    <span class="keyword">return</span> finalStr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/weiyayun1995/weiyayun1995.git/2021/06/18/%E8%84%B1%E6%95%8F/" data-id="ckq7mlsvo000pdgt914pr8t1o" data-title="脱敏" class="article-share-link">分享</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/06/18/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%88%A4%E6%96%AD/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">前一篇</strong>
      <div class="article-nav-title">
        
          js数据类型的判断
        
      </div>
    </a>
  
  
    <a href="/2021/06/18/Set-Map%E5%AE%9E%E7%8E%B0%E6%95%B0%E7%BB%84%E5%8E%BB%E9%87%8D/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">后一篇</strong>
      <div class="article-nav-title">Set Map实现数组去重</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">六月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/06/22/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E8%BD%AC%E6%8D%A2/">js数据类型及转换</a>
          </li>
        
          <li>
            <a href="/2021/06/18/react-hook-%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%AE%9E%E8%B7%B5/">react-hook 项目中实践</a>
          </li>
        
          <li>
            <a href="/2021/06/18/%E6%95%B0%E5%AD%97%E7%AD%BE%E5%90%8D/">数字签名</a>
          </li>
        
          <li>
            <a href="/2021/06/18/sendbeacon/">sendbeacon</a>
          </li>
        
          <li>
            <a href="/2021/06/18/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%88%A4%E6%96%AD/">js数据类型的判断</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 yywei<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>